Imports Microsoft.VisualBasic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections.Generic
Imports EntityLayer

Public Class SolicitudPedidoDA

    Public Shared Function ObtenerNuevoCodigo() As Integer

        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myCommand As New SqlCommand("COM_SolicitudPedido_GenClave", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure

        Dim para As SqlParameter

        para = New SqlParameter("@intClaveGenerada", SqlDbType.Int)
        para.Direction = ParameterDirection.Output
        myCommand.Parameters.Add(para)

        myConnection.Open()

        myCommand.ExecuteNonQuery()

        myConnection.Close()

        Return myCommand.Parameters("@intClaveGenerada").Value

    End Function

    Public Shared Function Insertar(ByVal OBE As SolicitudPedidoBE) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("COM_SolicitudPedido_Add", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@strNumeroPedido", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.Numero
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@dtFechaEmision", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.FechaEmision
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strClasificacion", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.Clasificacion
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strObservacion", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.Observacion
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@intIdSolicitud", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.IdSolicitud
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strSolicitudReq", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.strSolicitudReq
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True

        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function Actualizar(ByVal OBE As SolicitudPedidoBE) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("COM_SolicitudPedido_Upd", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@intIdSolicitud", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.Numero
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@dtFechaEmision", SqlDbType.DateTime)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.FechaEmision
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strClasificacion", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.Clasificacion
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strObservacion", SqlDbType.VarChar, 100)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.Observacion
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strSolicitudReq", SqlDbType.VarChar, 5)
            para.Direction = ParameterDirection.Input
            para.Value = OBE.strSolicitudReq
            myCommand.Parameters.Add(para)


            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True

        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function ListarTodo() As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("COM_SolicitudPedido_ListAll", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        myDataadapter.Fill(ds, "SolicitudPedido")

        Return ds

    End Function

    Public Shared Function ListarxCodigo(ByVal intIdSolicitud As Integer) As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("COM_SolicitudPedido_ListByCodigo", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        Dim para As SqlParameter

        para = New SqlParameter("@intIdSolicitud", SqlDbType.Int)
        para.Direction = ParameterDirection.Input
        para.Value = intIdSolicitud
        myDataadapter.SelectCommand.Parameters.Add(para)

        myDataadapter.Fill(ds, "SolicitudPedido")

        Return ds

    End Function

    Public Shared Function Eliminar(ByVal intIdSolicitud As Integer) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("COM_SolicitudPedido_Del", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@intIdSolicitud", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = intIdSolicitud
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function ActualizarEstado(ByVal intIdSolicitud As Integer, ByVal strEstado As String) As Boolean
        Try
            Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
            Dim myCommand As New SqlCommand("COM_SolicitudPedido_UpdEstado", myConnection)
            myCommand.CommandType = CommandType.StoredProcedure

            Dim para As SqlParameter

            para = New SqlParameter("@intIdSolicitud", SqlDbType.Int)
            para.Direction = ParameterDirection.Input
            para.Value = intIdSolicitud
            myCommand.Parameters.Add(para)

            para = New SqlParameter("@strEstado", SqlDbType.Char, 1)
            para.Direction = ParameterDirection.Input
            para.Value = strEstado
            myCommand.Parameters.Add(para)

            myConnection.Open()
            myCommand.ExecuteNonQuery()
            myConnection.Close()

            Return True
        Catch ex As Exception
            Dim E As String = ex.Message
            Return False
        End Try

    End Function

    Public Shared Function ListarTodoxEstado(ByVal chEstado As Char) As DataSet
        Dim myConnection As New SqlConnection(ConexionDA.ObtenerCnx)
        Dim myDataadapter As New SqlDataAdapter("COM_SolicitudPedido_ListAllByEstado", myConnection)
        myDataadapter.SelectCommand.CommandType = CommandType.StoredProcedure

        Dim ds As New DataSet

        Dim para As SqlParameter

        para = New SqlParameter("@chEstado", SqlDbType.Char)
        para.Direction = ParameterDirection.Input
        para.Value = chEstado
        myDataadapter.SelectCommand.Parameters.Add(para)

        myDataadapter.Fill(ds, "SolicitudPedido")

        Return ds
    End Function

End Class
